<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="回收ID" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入回收id"
          clearable
          
        />
      </el-form-item>
      <!-- <el-form-item label="支付运费订单号" prop="payNum">
        <el-input
          v-model="queryParams.payNum"
          placeholder="请输入支付运费订单号"
          clearable
          
        />
      </el-form-item> -->
      <el-form-item label="发货批次号" prop="sendNum">
        <el-input
          v-model="queryParams.sendNum"
          placeholder="请输入发货批次号"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="订单id" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单id"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="商品id" prop="goodslistId">
        <el-input
          v-model="queryParams.goodslistId"
          placeholder="请输入商品id"
          clearable
          
        />
      </el-form-item>

      <el-form-item label="商品名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入商品名称"
          clearable
          
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:list:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:list:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="listList"
      @selection-change="handleSelectionChange"
       @sort-change="dataListSortChangeHandle"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户" align="center" prop="username" width="200">
        <template slot-scope="scope">
          <div
            style=" display:flex;  justify-content: center; align-items: center"
          >
            <el-avatar :src="scope.row.headimg"></el-avatar>
           <div>{{ scope.row.username }}</div> 
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="订单号" align="center" prop="orderNum" /> -->
      <!-- <el-table-column label="支付运费订单号" align="center" prop="payNum" /> -->
      <el-table-column sortable="custom" label="订单id" align="center" prop="orderId" />
      <!-- <el-table-column label="商品id" align="center" prop="goodslistId" /> -->
      <el-table-column label="商品封面图" align="center" prop="imgurl">
        <template slot-scope="scope">
          <img :src="scope.row.imgurl" style="width: 50px; height: 50px" />
        </template>
      </el-table-column>
      <el-table-column sortable="custom" label="商品价格" align="center" prop="price" />
      <el-table-column  label="商品名称" align="center" prop="title" />
      <el-table-column label="下单数量" sortable="custom" align="center" prop="num" />
      <el-table-column sortable="custom" label="回收金额" align="center" prop="outPrice" />
      <el-table-column sortable="custom" label="回收状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 5">已回收</el-tag>
        </template>
      </el-table-column>

      <!-- <el-table-column label="收货人姓名" align="center" prop="name" />
      <el-table-column label="收货人电话" align="center" prop="mobile" />
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="县 " align="center" prop="county" />
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="快递单号" align="center" prop="courierNumber" />
      <el-table-column label="快递名称" align="center" prop="courierName" />
      <el-table-column label="回收时间/发货时间" align="center" prop="endTime" /> -->
      <el-table-column label="发货批次号" align="center" prop="sendNum" />
      <!-- <el-table-column label="订单备注" align="center" prop="message" /> -->
      <!-- <el-table-column label="是否出售 1 是 2 否" align="center" prop="isSale" />
      <el-table-column label="出售时间" align="center" prop="saleTime" /> -->
      <el-table-column sortable="custom" label="商品id" align="center" prop="goodsId" />
      <el-table-column sortable="custom" label="订单类型" align="center" prop="orderType">
        <template slot-scope="scope">
          <el-tag
            :type="
              scope.row.orderType == 1
                ? 'success'
                : scope.row.orderType == 2
                ? 'warning'
                : scope.row.orderType == 3
                ? 'info'
                : scope.row.orderType == 4
                ? 'danger'
                : 'primary'
            "
            >{{
              scope.row.orderType == 1
                ? "下单流程"
                : scope.row.orderType == 2
                ? "集市获得"
                : scope.row.orderType == 3
                ? "积分兑换"
                : scope.row.orderType == 4
                ? "积分抽奖"
                : "未知"
            }}</el-tag
          >
        </template>
      </el-table-column>
      <!-- <el-table-column label="0其他 1普通抽奖 2first last w 最终" align="center" prop="kjIs" />
      <el-table-column label="0其他 1普通抽奖 2first last w 最终" align="center" prop="jfKjIs" /> -->
      <!-- <el-table-column label="抽奖序号" align="center" prop="luckNo" />
      <el-table-column label="0否 1是保底" align="center" prop="minType" />
      <el-table-column label="0普通 1无限赏赠送" align="center" prop="give" /> -->
      <!-- <el-table-column label="1保险柜" align="center" prop="insurance" /> -->
      <!-- <el-table-column label="${comment}" align="center" prop="creator" /> -->
      <el-table-column
        label="下单时间"
        align="center"
        prop="addtime"
        width="180"
        sortable="custom"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.addtime, "{y}-{m}-{d} {hh}:{mm}:{ss}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:list:edit']"
            >查看详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:list:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="${comment}" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="订单号" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="支付运费订单号" prop="payNum">
          <el-input v-model="form.payNum" placeholder="请输入支付运费订单号" />
        </el-form-item>
        <el-form-item label="订单id" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单id" />
        </el-form-item>
        <el-form-item label="商品id" prop="goodslistId">
          <el-input v-model="form.goodslistId" placeholder="请输入商品id" />
        </el-form-item>
        <el-form-item label="商品封面图" prop="imgurl">
          <el-input v-model="form.imgurl" placeholder="请输入商品封面图" />
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入商品价格" />
        </el-form-item>
        <el-form-item label="商品名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="下单数量" prop="num">
          <el-input v-model="form.num" placeholder="请输入下单数量" />
        </el-form-item>
        <el-form-item label="回收金额" prop="outPrice">
          <el-input v-model="form.outPrice" placeholder="请输入回收金额" />
        </el-form-item>
        <el-form-item label="下单时间" prop="addtime">
          <el-input v-model="form.addtime" placeholder="请输入下单时间" />
        </el-form-item>
        <el-form-item label="收货人姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入收货人姓名" />
        </el-form-item>
        <el-form-item label="收货人电话" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入收货人电话" />
        </el-form-item>
        <el-form-item label="省" prop="province">
          <el-input v-model="form.province" placeholder="请输入省" />
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-input v-model="form.city" placeholder="请输入市" />
        </el-form-item>
        <el-form-item label="县 " prop="county">
          <el-input v-model="form.county" placeholder="请输入县 " />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="快递单号" prop="courierNumber">
          <el-input v-model="form.courierNumber" placeholder="请输入快递单号" />
        </el-form-item>
        <el-form-item label="快递名称" prop="courierName">
          <el-input v-model="form.courierName" placeholder="请输入快递名称" />
        </el-form-item>
        <el-form-item label="回收时间/发货时间" prop="endTime">
          <el-input
            v-model="form.endTime"
            placeholder="请输入回收时间/发货时间"
          />
        </el-form-item>
        <el-form-item label="发货批次号" prop="sendNum">
          <el-input v-model="form.sendNum" placeholder="请输入发货批次号" />
        </el-form-item>
        <el-form-item label="订单备注" prop="message">
          <el-input v-model="form.message" placeholder="请输入订单备注" />
        </el-form-item>
        <el-form-item label="是否出售 1 是 2 否" prop="isSale">
          <el-input
            v-model="form.isSale"
            placeholder="请输入是否出售 1 是 2 否"
          />
        </el-form-item>
        <el-form-item label="出售时间" prop="saleTime">
          <el-input v-model="form.saleTime" placeholder="请输入出售时间" />
        </el-form-item>
        <el-form-item label="商品id" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入商品id" />
        </el-form-item>
        <el-form-item label="0其他 1普通抽奖 2first last w 最终" prop="kjIs">
          <el-input
            v-model="form.kjIs"
            placeholder="请输入0其他 1普通抽奖 2first last w 最终"
          />
        </el-form-item>
        <el-form-item label="0其他 1普通抽奖 2first last w 最终" prop="jfKjIs">
          <el-input
            v-model="form.jfKjIs"
            placeholder="请输入0其他 1普通抽奖 2first last w 最终"
          />
        </el-form-item>
        <el-form-item label="抽奖序号" prop="luckNo">
          <el-input v-model="form.luckNo" placeholder="请输入抽奖序号" />
        </el-form-item>
        <el-form-item label="0普通 1无限赏赠送" prop="give">
          <el-input v-model="form.give" placeholder="请输入0普通 1无限赏赠送" />
        </el-form-item>
        <el-form-item label="1保险柜" prop="insurance">
          <el-input v-model="form.insurance" placeholder="请输入1保险柜" />
        </el-form-item>
        <el-form-item label="${comment}" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="createDate">
          <el-date-picker
            clearable
            v-model="form.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择${comment}"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listList,
  listList_old,
  getList,
  delList,
  addList,
  updateList,
} from "@/api/order/order_list.js";
import { getMy_user } from "@/api/users/user.js";
export default {
  name: "List",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单明细表格数据
      listList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
             isAsc: 'desc',                  // 排序，asc／desc
      orderByColumn: 'addtime',             // 排序，字段
        pageNum: 1,
        pageSize: 10,
        userId: null,
        orderNum: null,
        payNum: null,
        orderId: null,
        goodslistId: null,
        imgurl: null,
        price: null,
        title: null,
        num: null,
        outPrice: null,
        status: 5,
        addtime: null,
        name: null,
        mobile: null,
        province: null,
        city: null,
        county: null,
        address: null,
        courierNumber: null,
        courierName: null,
        endTime: null,
        sendNum: null,
        message: null,
        isSale: null,
        saleTime: null,
        goodsId: null,
        orderType: null,
        kjIs: null,
        jfKjIs: null,
        luckNo: null,
        minType: null,
        give: null,
        insurance: null,
        creator: null,
        createDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNum: [
          { required: true, message: "订单号不能为空", trigger: "blur" },
        ],
        goodslistId: [
          { required: true, message: "商品id不能为空", trigger: "blur" },
        ],
        imgurl: [
          { required: true, message: "商品封面图不能为空", trigger: "blur" },
        ],
        price: [
          { required: true, message: "商品价格不能为空", trigger: "blur" },
        ],
        title: [
          { required: true, message: "商品名称不能为空", trigger: "blur" },
        ],
        num: [{ required: true, message: "下单数量不能为空", trigger: "blur" }],
        status: [
          {
            required: true,
            message:
              "1 待选择发货 回收  2 待发货  3 已发货 4 已收货 5已回收不能为空",
            trigger: "blur",
          },
        ],
        addtime: [
          { required: true, message: "下单时间不能为空", trigger: "blur" },
        ],
        kjIs: [
          {
            required: true,
            message: "0其他 1普通抽奖 2first last w 最终不能为空",
            trigger: "blur",
          },
        ],
        jfKjIs: [
          {
            required: true,
            message: "0其他 1普通抽奖 2first last w 最终不能为空",
            trigger: "blur",
          },
        ],
        luckNo: [
          { required: true, message: "抽奖序号不能为空", trigger: "blur" },
        ],
        minType: [
          { required: true, message: "0否 1是保底不能为空", trigger: "change" },
        ],
        give: [
          {
            required: true,
            message: "0普通 1无限赏赠送不能为空",
            trigger: "blur",
          },
        ],
        insurance: [
          { required: true, message: "1保险柜不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单明细列表 */
    getList() {
      this.loading = true;
      listList(this.queryParams).then(async (response) => {
        this.listList = response.rows;
        this.queryUser(this.listList);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        orderNum: null,
        payNum: null,
        orderId: null,
        goodslistId: null,
        imgurl: null,
        price: null,
        title: null,
        num: null,
        outPrice: null,
        status: 5,
        addtime: null,
        name: null,
        mobile: null,
        province: null,
        city: null,
        county: null,
        address: null,
        courierNumber: null,
        courierName: null,
        endTime: null,
        sendNum: null,
        message: null,
        isSale: null,
        saleTime: null,
        goodsId: null,
        orderType: null,
        kjIs: null,
        jfKjIs: null,
        luckNo: null,
        minType: null,
        give: null,
        insurance: null,
        creator: null,
        createDate: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.sendNum || this.ids;
      //跳转到详情页
      this.$tab.openPage(
        "查看[" + id + "]订单详情",
        "/order/order/list_xq/index/" + id
      );
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateList(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addList(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除订单明细编号为"' + ids + '"的数据项？')
        .then(function () {
          return delList(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/list/export",
        {
          ...this.queryParams,
        },
        `list_${new Date().getTime()}.xlsx`
      );
    },
    //查询用户信息
    async queryUser(arr) {
      let val = arr;
      for (let index = 0; index < arr.length; index++) {
        console.log(arr[index].userId);
        await getMy_user(arr[index].userId).then((response) => {
          if(response.data){
          this.$set(this.listList[index], "username", response.data.nickname);
          this.$set(this.listList[index], "headimg", response.data.headimg);
          }
        });
      }
    },
      //排序
    dataListSortChangeHandle(data) {
       if (!data.order || !data.prop) {
        this.queryParams.orderByColumn = ''
        this.queryParams.isAsc = ''
        return false
      }
     this.queryParams.isAsc = data.order.replace(/ending$/, '')
       this.queryParams.orderByColumn = data.prop.replace(/([A-Z])/g, '_$1').toLowerCase()
       this.getList();
    },
  },
};
</script>
